#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include "../glutil/PortableGL.h"
#include <QGLWidget>
#include <memory>
#include "../geom/TerrainModel.h"
#include "../glutil/TerrainRenderer.h"
#include "../dataengine/TerrainFileManager.h"
#include "../glutil/FreeCamera.h"
namespace voronoi
{
    class VoronoiRenderer;
}
class MainWidget : public QGLWidget
{
    Q_OBJECT
    public:
        MainWidget(QWidget * parentwidget);
        void retrieveTriangulated(voronoi::VoronoiRenderer * vrenderer);
        void disableRender();
        void enableRender();
    public slots:
        void doOpenFile();
        void doImportFile();
        void doSaveFile();
        void doTriangulate();
        void setWireframeTerrain(bool set);
        void setVoronoiCellSize(double cellSize);
        void setVoronoiDepthDivisor(double divisor);
        void setVoronoiMaxDensity(double density);
        void newTerrainDimensions(float width, float length, float height);

        void mouseMoveEvent(QMouseEvent * event);
        void mousePressEvent(QMouseEvent * event);
        void wheelEvent(QWheelEvent * event);
    signals:
        void fileChanged(QString f);
        void terrainDimensionsChanged(float width, float length, float height);

    protected:
        bool m_wireframeTerrain;
        glutil::FreeCamera m_camera;
        QPoint lastPos;
        float voronoiCellSize;
        float depthDivisor;
        float voronoiMaxDensity;
        float rotx;
        float roty;
        geom::FittedBBox m_currentModelBBox;
        geom::Vector3D displayScale;
        geom::Vector3D desiredTerrainSize;
        geom::Point3D displayCenter;

        geom::TerrainModel m_currentModel;

        std::auto_ptr<glutil::TerrainRenderer> m_currentTerrainRenderer;
        void paintGL();
        void resizeGL(int w, int h);
        void setGLLight();
        void initializeGL();
        void updatePointsBBox();
        void updateDisplayScale();
        bool m_renderEnabled;
};

#endif // MAINWIDGET_H
